package com.youlu.campus.service.data.impl;

import com.alibaba.fastjson.JSON;
import com.youlu.campus.base.exception.BusinessException;
import com.youlu.campus.common.utils.TimeUtils;
import com.youlu.campus.entity.ActivityDauData;
import com.youlu.campus.service.data.ActivityDauDataService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

@Slf4j
@Service
public class ActivityDauDataServiceImpl implements ActivityDauDataService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public ActivityDauData find(String userId, String activityId, Integer bizType) {
        log.info(":>>> 开始查找活动DAU userId:{},activityId:{},bizType:{}", userId, activityId, bizType);
        if (StringUtils.isBlank(userId) || StringUtils.isBlank(activityId) || Objects.isNull(bizType)) {
            log.error(":>>> 活动DAU操作错误");
            throw new BusinessException("活动DAU操作错误");
        }
        Date day = TimeUtils.getDay0Time();
        Query query = new Query().addCriteria(Criteria.where("userId").is(userId).and("dayTime").is(day).and("activityId").is(activityId).and("bizType").is(bizType));
        ActivityDauData a = mongoTemplate.findOne(query, ActivityDauData.class);
        return a;
    }

    @Async
    @Override
    public void create(ActivityDauData data) {
        Date date = new Date();
        Date day = TimeUtils.getDay0Time();
        data.setId(null);
        data.setCreatedTime(date);
        data.setDayTime(day);
        mongoTemplate.insert(data);
    }

    @Async
    @Override
    public void update(ActivityDauData data) {
        log.info(":>>> 开始修改用户DAU:{}", JSON.toJSONString(data));
        Query query = new Query().addCriteria(Criteria.where("id").is(data.getId()));
        Update update = new Update();
        update.inc("num", data.getNum());
        update.set("updatedTime", new Date());
        mongoTemplate.updateFirst(query, update, ActivityDauData.class);
    }

    @Async
    @Override
    public void incr(String userId, String activityId, Integer bizType) {
        log.info(":>>> 开始累加活动DAU数据:{},{},{},", userId, activityId, bizType);
        ActivityDauData data = this.find(userId, activityId, bizType);
        if (Objects.isNull(data)) {
            data = new ActivityDauData();
            Date day = TimeUtils.getDay0Time();
            data.setDayTime(day);
            data.setUserId(userId);
            data.setActivityId(activityId);
            data.setBizType(bizType);
            data.setNum(1);
            data.setFirstTime(new Date());
            this.create(data);
        }
        data.setNum(1);
        update(data);

    }
}
